import scrapy
from scrapy import Request
from scrapy_splash import SplashRequest

from jayce.items import PictureItem


class PictureSpider(scrapy.Spider):
    name = 'picture'
    allowed_domains = ['pic.netbian.com']
    start_urls = ['https://pic.netbian.com/4kfengjing/']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url=url, callback=self.parse)

    def parse(self, response):
        picture_url = [p_url.extract() for p_url in response.xpath("//div[@class='slist']//ul/li/a/@href")]
        picture_description = [p_desc.extract() for p_desc in
                               response.xpath("//div[@class='slist']//li//a//text()")]

        picture_dict = dict(zip(picture_url, picture_description))
        for k, v in picture_dict.items():
            if k.startswith('/4k'):
                yield SplashRequest(url=response.urljoin(k), callback=self.parse)
            else:
                yield PictureItem(picture_url=response.urljoin(k), picture_description=v)
